﻿using Facebook.Unity;
using System.Collections.Generic;
using UnityEngine;

namespace HWGames.Bundles.Internal.Analytics { 
    public class FBMgr : Singleton<FBMgr> {

        public bool isInit = false;

        List<string> storedEvent = new List<string>();
        List<KeyValuePair<string, Dictionary<string, object>>> storedComplexEvent = new List<KeyValuePair<string, Dictionary<string, object>>>();

        public void Init() {
            if (!FB.IsInitialized) {
                FB.Init(InitCallback, OnHideUnity);
            }
            else
                FB.ActivateApp();
        }

        private void InitCallback() {
            isInit = true;
            if (FB.IsInitialized) {
                FB.ActivateApp();
                TrackStoredEvent();
                HWLog.Log("HWSDKBundle", "Initializing Facebook");
            }
            else
                Debug.LogError("Failed to initialize the Facebook SDK");
        }

        private void OnHideUnity(bool isGameShown) {
            if (!isGameShown) {
                // Pause the game - we will need to hide
                Time.timeScale = 0;
            }
            else {
                // Resume the game - we're getting focus again
                Time.timeScale = 1;
            }
        }

        public void Log_FB_Event(string eventName, string param = null, object obj = null) {
            if (obj == null || param == null) {
                FBMgr.Instance.TrackEvent(eventName);
#if UNITY_EDITOR
                string log = string.Format("【FB_Log_Event】:{0} ", eventName);
                if (!log.Contains("display")) {
                    Debug.Log(log);
                }
#endif
            }
            else {
                Dictionary<string, object> dic = new Dictionary<string, object>();
                dic[param] = obj;
                FBMgr.Instance.TrackEvent(eventName, dic);
#if UNITY_EDITOR
                string log = string.Format("【FB_Log_Event】:{0}:{1}:{2}", eventName, param, obj.ToString());
                if (!log.Contains("display")) {
                    Debug.Log(log);
                }
#endif
            }
        }

        public void Log_FB_Event(string eventName, Dictionary<string, object> dic) {
            if (dic == null) {
                FBMgr.Instance.TrackEvent(eventName);
#if UNITY_EDITOR
                string log = string.Format("【FB_Log_Event】:{0} ", eventName);
                if (!log.Contains("display")) {
                    Debug.Log(log);
                }
#endif
            }
            else {
                FBMgr.Instance.TrackEvent(eventName, dic);
#if UNITY_EDITOR
                string log = string.Format("【FB_Log_Event】:{0}}", eventName);
                if (!log.Contains("display")) {
                    Debug.Log(log);
                }
#endif
            }
        }

        private void TrackEvent(string eventName, Dictionary<string, object> eventParam = null) {
            if (!FB.IsInitialized) {
                if (eventParam != null) {
                    KeyValuePair<string, Dictionary<string, object>> nEvent =
                     new KeyValuePair<string, Dictionary<string, object>>(eventName, eventParam);
                    storedComplexEvent.Add(nEvent);
                }
                else
                    storedEvent.Add(eventName);
            }
            else {
#if !UNITY_EDITOR
            if (eventParam != null) {
                FB.LogAppEvent(eventName, null, eventParam);
            }
            else {
                FB.LogAppEvent(eventName);
            }
#endif
            }
        }

        private void TrackStoredEvent() {
            for (int c = 0; c < storedEvent.Count; c++) {
                TrackEvent(storedEvent[c]);
            }
            storedEvent.Clear();
            for (int c = 0; c < storedComplexEvent.Count; c++) {
                TrackEvent(storedComplexEvent[c].Key, storedComplexEvent[c].Value);
            }
            storedComplexEvent.Clear();
        }
    }
}